package com.big.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.big.app.dao.FoodOrderDao;
import com.big.app.entity.FoodOrder;
import com.big.app.service.FoodOrderService;
import com.big.app.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class FoodOrderServiceImpl implements FoodOrderService {
    @Autowired
    private FoodOrderDao dao;
    @Override
    @Transactional
    public R foodOrder(String name, double price) {
        dao.insert(new FoodOrder(name, createNo(), price));
        QueryWrapper<FoodOrder> queryWrapper =new QueryWrapper();
        queryWrapper.eq("name",name);
        FoodOrder foodOrder = dao.selectOne(queryWrapper);
        Long id = foodOrder.getId();
        return new R(200,"ok",id);
    }
    //生成订单号，不超过64位
    private String createNo(){
        return System.currentTimeMillis()+"_"+new Random().nextInt(1000);
    }
}
